package com.cyh.testdesignpattern.observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WeatherData  implements Subject{
    private float temperature;
    private float pressure;
    private float humidify;

    List<Observer> observerList;

    public WeatherData() {
        observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer o) {
        if(!observerList.contains(o)) {
            observerList.add(o);
        }
    }

    @Override
    public void removeObserver(Observer o) {
        if(observerList.contains(o)) {
            observerList.remove(o);
        }
    }

    @Override
    public void notifyObservers() {
        Iterator<Observer> iterator = observerList.iterator();
        while (iterator.hasNext()) {
            Observer observer = iterator.next();
            observer.update(temperature, pressure, humidify);
        }
    }

    //数据变化
    public void setData(float temperature, float pressure, float humidify) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidify = humidify;
        dataChange();
    }

    public void dataChange() {
        notifyObservers();
    }


    public float getTemperature() {
        return temperature;
    }

    public void setTemperature(float temperature) {
        this.temperature = temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public void setPressure(float pressure) {
        this.pressure = pressure;
    }

    public float getHumidify() {
        return humidify;
    }

    public void setHumidify(float humidify) {
        this.humidify = humidify;
    }
}
